import 'package:flutter/material.dart';
import 'package:Lter/screens/lters/server.dart';
import 'package:Lter/screens/lters/client.dart';
import 'package:Lter/model/lter.dart';
import 'package:Lter/model/app.dart';
import 'package:Lter/util/toast.dart';

class NavLtersScreen extends StatefulWidget {
  NavLtersScreen({Key key}) : super(key: key);

  @override
  createState() => _NavLtersScreenState();
}

class _NavLtersScreenState extends State<NavLtersScreen> {
  LterServerInfo mLterServerInfo = LterServerInfo();
  List<LterClientInfo> mLterClientInfoList = List<LterClientInfo>();
  bool mGetLterSuss = false;

  @override
  void deactivate() {
    AppInherited app = AppContainer.of(context).app;
    app.mWebSocket.Send("lter_stop", app.mUser, null);
    app.mWebSocket.RemoveHandle("lter");
    super.deactivate();
  }

  void UpdateData() {
    AppInherited app = AppContainer.of(context).app;

    if (mGetLterSuss) {
      app.mLterServerInfo = mLterServerInfo;
      app.mLterClientInfoList = mLterClientInfoList;
    } else {
      mLterServerInfo = app.mLterServerInfo;
      mLterClientInfoList = app.mLterClientInfoList;

      app.mWebSocket.AddHandle("lter", (jsonMap) {
        LterServerInfo tLterServerInfo = mLterServerInfo;

        tLterServerInfo.MemUse = jsonMap["memory"]["used_percent"];
        tLterServerInfo.CpuNum = jsonMap["cpu"]["cpu_num"];
        tLterServerInfo.CpuUse = jsonMap["cpu"]["used_percent"];
        tLterServerInfo.NetUp = jsonMap["net"]["up"];
        tLterServerInfo.NetDown = jsonMap["net"]["down"];

        List<LterClientInfo> tLterClientInfoList = List<LterClientInfo>();

        if (jsonMap["client"] != null) {
          int jsonMapClientLen = jsonMap["client"].length;

          for (int i = 0; i < jsonMapClientLen; i++) {
            LterClientInfo tLterClientInfo = LterClientInfo();
            tLterClientInfo.Addr = jsonMap["client"][i]["addr"];
            tLterClientInfo.IPRegion = jsonMap["client"][i]["ip_region"];
            tLterClientInfo.OnTime = jsonMap["client"][i]["on_time"];
            tLterClientInfo.PcapCount = jsonMap["client"][i]["pcap_count"];

            tLterClientInfoList.add(tLterClientInfo);
          }
        } else if (!mGetLterSuss) {
          Toast.snack(context, 'No 4G Lter was found !');
        }

        mGetLterSuss = true;

        setState(() {
          mLterServerInfo = tLterServerInfo;
          mLterClientInfoList = tLterClientInfoList;
        });
      });

      app.mWebSocket.Send("lter_start", app.mUser, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    UpdateData();

    return Scaffold(
      appBar: AppBar(
        title: Text('Health Status'),
      ),
      body: Column(
        children: <Widget>[
          healthCard(LterServerCard(mLterServerInfo: mLterServerInfo)),
          Expanded(
              child: ListView.builder(
                  padding: const EdgeInsets.all(0.0),
                  itemCount: mLterClientInfoList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return healthCard(LterClientCard(
                        mLterClientInfo: mLterClientInfoList[index]));
                  })),
        ],
      ),
    );
  }

  Widget healthCard(Widget w) {
    return Card(
      //color: color,
      //z轴的高度，设置card的阴影
      elevation: 25.0,
      //设置shape，这里设置成了R角
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
      ),
      //对Widget截取的行为，比如这里 Clip.antiAlias 指抗锯齿
      clipBehavior: Clip.antiAlias,
      semanticContainer: false,
      child: w,
    );
  }
}
